﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;

namespace MicropleSoft.WmsServiceLibrary.Cache
{
    public class DataCacheManager
    {
        //读取默认的CacheManager,CacheManager内部创建一个Cache对象，Cache内部维护一个Hashtable.
        public const string SessionKey = "SessionID:{0}";
        public const string UserIdKey = "I:{0}";
        public const string UserNameKey = "N:{0}";
        private static readonly ICacheManager CacheManager = CacheFactory.GetCacheManager();

        public static void SetSessionKey(string sessionId)
        {
            var key = string.Format(SessionKey, sessionId);
            CacheManager.Add(key, key);
        }

        public static bool ContainSessionKey(string sessionId)
        {
            var key = string.Format(SessionKey, sessionId);
            if (CacheManager.Contains(key))
            {
                CacheManager.GetData(key);
                return true;
            }
            return false;
        }

        public static void SetUserId(string sessionId, string userId)
        {
            var key = string.Format(UserIdKey, sessionId);
            CacheManager.Add(key, userId);
        }

        public static string GetUserId(string sessionId)
        {
            var key = string.Format(UserIdKey, sessionId);
            return (String)CacheManager.GetData(key);
        }

        public static void SetUserName(string sessionId, string userName)
        {
            var key = string.Format(UserNameKey, sessionId);
            CacheManager.Add(key, userName);
        }

        public static string GetUserName(string sessionId)
        {
            var key = string.Format(UserNameKey, sessionId);
            return (String)CacheManager.GetData(key);
        }
    }
}
